package cn.majingjing.core.tool;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresolver.FileTemplateResolver;

/**
 * @author majinding888@foxmail.com
 */
public class ThymeleafUtils {
	private static Logger log = LoggerFactory.getLogger(ThymeleafUtils.class);

	private static FileTemplateResolver r = new FileTemplateResolver();
	static {
		r.setTemplateMode(TemplateMode.HTML);
		r.setCharacterEncoding("utf-8");
	}

	public static String render(Map<String, Object> data, String templateFile) {
		log.info("开始渲染:{}", templateFile);
		TemplateEngine t = new TemplateEngine();
		t.setTemplateResolver(r);
		Context c = new Context();
		c.setVariables(data);
		String s = t.process(templateFile, c);
		log.info("渲染结果:\n{}", s);
		return s;
	}

}
